package zheezes.protocol.impl.gbw.afn8d;

import java.util.ArrayList;
import java.util.List;

import zheezes.protocol.impl.gbw.DataUnit;
import zheezes.protocol.impl.gbw.bean.DailyFrozenData;
import zheezes.util.ByteUtil;


public class Afn8DUpF1DataUnit extends DataUnit {
	private short num;
	private List<DailyFrozenData> list = new ArrayList<DailyFrozenData>();

	@Override
	public int sizeOfData() {
		int size = 2;
		for (DailyFrozenData data : list) {
			size += data.sizeof();
		}
		return size;
	}

	@Override
	public int getData(byte[] buf, int offset) {
		int idx = offset;
		ByteUtil.stoc(buf, idx, num);
		idx += 2;
		for (DailyFrozenData data : list) {
			idx += data.getBytes(buf, idx);
		}

		return idx - offset;
	}

	@Override
	public int setData(byte[] buf, int offset, int max) {
		int idx = offset;
		num = ByteUtil.ctos(buf, idx);
		idx += 2;
		while(idx < max) {
			DailyFrozenData data = new DailyFrozenData();
			list.add(data);
			idx += data.setBytes(buf, idx);
		}
		
		return idx - offset;
	}
	
	public DailyFrozenData[] getDailyFrozenDatas() {
		return list.toArray(new DailyFrozenData[0]);
	}
}
